package com.waiting.leetcode;

import java.util.Arrays;

/**
 * @Description
 * @Author bjweitingting
 * @Date 2019/3/15
 **/
public class SingleNumberII137 {
    public static void main(String[] args) {

    }

    public int singleNumber(int[] nums) {
        //sort the array
        Arrays.sort(nums);

        int result = -1;
        boolean found = false;
        int i = 0;

        while (found == false) {
            found = false;

            if (i == nums.length - 1) {
                result = nums[i];
                found = true;
                break;
            }
            //if its next one, then we know three more of same number exist
            if (nums[i] == nums[i + 1]) {
                i += 3;
                continue;
            } else {
                //if not equal to the one next to it, we've found it.
                result = nums[i];
                found = true;
                break;
            }
        }
        return result;
    }
}
